package com.ruoyi.mobile.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mobile.domain.User;
import com.ruoyi.mobile.mapper.UserMapper;
import com.ruoyi.mobile.model.vo.UserNameVo;
import com.ruoyi.mobile.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;

/**
 * 用户Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-12
 */
@Service
public class UserServiceImpl implements IUserService
{
    @Autowired
    private UserMapper userMapper;

    /**
     * 查询用户
     * 
     * @param id 用户主键
     * @return 用户
     */
    @Override
    public User selectUserById(Integer id)
    {
        User user = userMapper.selectUserById(id);
        user.setOperationTime(null);
        user.setFatPercent(null);
        user.setFirstBindTime(null);
        user.setBindTime(null);
        user.setSn(null);
        return user;
    }

    /**
     * 查询用户列表
     * 
     * @param user 用户
     * @return 用户
     */
    @Override
    public List<User> selectUserList(User user)
    {
        return userMapper.selectUserList(user);
    }

    /**
     * 新增用户
     * 
     * @param user 用户
     * @return 结果
     */
    @Override
    public int insertUser(User user)
    {
        user.setCreateTime(DateUtils.getNowDate());
        if (StringUtils.isEmpty(user.getAvatar())) {
            user.setAvatar("https://qny.bravo.love/avatar/user.png");
        }
        return userMapper.insertUser(user);
    }

    /**
     * 修改用户
     * 
     * @param user 用户
     * @return 结果
     */
    @Override
    public int updateUser(User user)
    {
        user.setUpdateTime(DateUtils.getNowDate());
        return userMapper.updateUser(user);
    }

    /**
     * 批量删除用户
     * 
     * @param ids 需要删除的用户主键
     * @return 结果
     */
    @Override
    public int deleteUserByIds(Integer[] ids)
    {
        return userMapper.deleteUserByIds(ids);
    }

    /**
     * 删除用户信息
     * 
     * @param id 用户主键
     * @return 结果
     */
    @Override
    public int deleteUserById(Integer id)
    {
        return userMapper.deleteUserById(id);
    }

    @Override
    public List<UserNameVo> getAllUser(String keyword) {
        return userMapper.getAllUser(keyword);
    }


    /**
     * 根据手术时间判断某一天是第几周的第一天
     * @param operationTime
     * @param localDateTime
     * @return
     */
    @Override
    public Integer getWeekByDate(LocalDateTime operationTime, LocalDateTime localDateTime) {
        //手术时间是10.05
        //开始推荐课程时间是10.06
        //第一周就是10.06 - 10.12
        //第二周就是10.13 - 10.19
        //第三周就是10.20 - 10.26

        //手术后的第二天就是开始运动的时间
        LocalDate trainStartDate = LocalDateTimeUtil.offset(operationTime,1, ChronoUnit.DAYS).toLocalDate();
        //计算相差天数
        long betweenDays = LocalDateTimeUtil.between(trainStartDate.atStartOfDay(), LocalDateTime.now().toLocalDate().atStartOfDay(), ChronoUnit.DAYS);
        if (betweenDays >= 0 && betweenDays % 7 == 0) {
            Integer week = Math.toIntExact(betweenDays/7);
            week += 1;
            return week;
        }
        return null;
    }
}
